<!doctype html>
<html lang="en">
	<head>
		<title>three.js webgl - materials</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<script src="../build/Three.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/Stats.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;

			var camera, scene, renderer, objects;
			var particleLight, pointLight;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				scene = new THREE.Scene();

				camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set( 0, 200, 0 );
				scene.add( camera );

				// Materials

				var imgTexture2 = THREE.ImageUtils.loadTexture( "textures/planets/moon_1024.jpg" );

				var imgTexture = THREE.ImageUtils.loadTexture( "textures/lava/lavatile.jpg" );
				imgTexture.repeat.set( 4, 2 );
				imgTexture.wrapS = imgTexture.wrapT = THREE.RepeatWrapping;

				var shininess = 15, shading = THREE.SmoothShading;

				var materials = [];

				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0xffffff, ambient: 0x777777, specular: 0x999999, shininess: shininess, shading: shading } ) );
				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0x00ff00, ambient: 0x777777, specular: 0x999999, shininess: shininess, shading: shading } ) );
				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0x00ff00, ambient: 0x007700, specular: 0x999999, shininess: shininess, shading: shading } ) );
				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture, color: 0x000000, ambient: 0x00ff00, specular: 0x999999, shininess: shininess, shading: shading } ) );

				materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xffffff, ambient: 0x777777, shading: shading } ) );
				materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xff0000, ambient: 0x777777, shading: shading } ) );
				materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0xff0000, ambient: 0x770000, shading: shading } ) );
				materials.push( new THREE.MeshLambertMaterial( { map: imgTexture, color: 0x000000, ambient: 0xff0000, shading: shading } ) );

				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0xffaa00, shininess: shininess, metal: true, shading: shading } ) );
				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0xaaff00, shininess: shininess, metal: true, shading: shading } ) );
				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0x00ffaa, shininess: shininess, metal: true, shading: shading } ) );
				materials.push( new THREE.MeshPhongMaterial( { map: imgTexture2, color: 0x000000, ambient: 0x000000, specular: 0x00aaff, shininess: shininess, metal: true, shading: shading } ) );

				// Spheres geometry

				var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 );
				var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 );

				objects = [];

				var sphere, geometry, material;

				for ( var i = 0, l = materials.length; i < l; i ++ ) {

					material = materials[ i ];

					geometry = material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth;

					sphere = new THREE.Mesh( geometry, material );

					sphere.position.x = ( i % 4 ) * 200 - 200;
					sphere.position.z = Math.floor( i / 4 ) * 200 - 200;

					objects.push( sphere );

					scene.add( sphere );

				}

				particleLight = new THREE.Mesh( new THREE.SphereGeometry( 4, 8, 8 ), new THREE.MeshBasicMaterial( { color: 0xffffff } ) );
				scene.add( particleLight );

				// Lights

				scene.add( new THREE.AmbientLight( 0x444444 ) );

				var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
				directionalLight.position.set( 1, 1, 1 ).normalize();
				scene.add( directionalLight );

				pointLight = new THREE.PointLight( 0xffffff, 2, 800 );
				scene.add( pointLight );

				particleLight.material.color = pointLight.color;
				pointLight.position = particleLight.position;

				//

				renderer = new THREE.WebGLRenderer( { clearColor: 0x0a0a0a, clearAlpha: 1, antialias: true } );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.sortObjects = true;

				container.appendChild( renderer.domElement );

				renderer.gammaInput = true;
				renderer.gammaOutput = true;
				//renderer.physicallyBasedShading = true;

				//

				stats = new Stats();
				stats.domElement.style.position = 'absolute';
				stats.domElement.style.top = '0px';

				container.appendChild( stats.domElement );

			}

			//

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				var timer = Date.now() * 0.00025;

				camera.position.x = Math.cos( timer ) * 800;
				camera.position.z = Math.sin( timer ) * 800;

				camera.lookAt( scene.position );

				for ( var i = 0, l = objects.length; i < l; i ++ ) {

					var object = objects[ i ];

					object.rotation.y += 0.005;

				}

				particleLight.position.x = Math.sin( timer * 7 ) * 300;
				particleLight.position.y = Math.cos( timer * 5 ) * 400;
				particleLight.position.z = Math.cos( timer * 3 ) * 300;

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
